package com.intel.two.controller.camera.outdoor;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.builder.ExcelReaderBuilder;
import com.alibaba.excel.read.builder.ExcelReaderSheetBuilder;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.alibaba.excel.write.builder.ExcelWriterSheetBuilder;
import com.intel.two.bo.camera.CameraBo;
import com.intel.two.common.LayuiPageVo;
import com.intel.two.common.R;
import com.intel.two.listener.camera.WebFloorListener;
import com.intel.two.pojo.camera.floor.FloorCameraPojo;
import com.intel.two.service.camera.outdoor.FctServiceO;
import com.intel.two.utils.OssUtil2;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/floorTableP")
public class FloorCameraTableO {
    @Autowired
    @Qualifier("fctServiceImplO")
    private FctServiceO fctService;

    @Autowired
    @Qualifier("webFloorListener")
    WebFloorListener webFloorListener;

    @RequestMapping("/findEveryFloor")
    public LayuiPageVo findEveryFloor (@RequestParam Map<Object,Object> map) {
        System.out.println(map);
        return fctService.findEveryFloor(map);
    }

    @RequestMapping("/delSpecifiedFloor")
    public R delSpecifiedFloor (String id) {
        System.out.println("得到需要删除的id："+id);
        return fctService.delSpecifiedFloor(id);
    }

    @RequestMapping("/addFloor")
    public R addFloor (FloorCameraPojo floorCameraPojo) {
        System.out.println("添加完整的监控" + floorCameraPojo);
        return fctService.addFloor(floorCameraPojo);
    }

    @RequestMapping("/uploadFloor")
    public R uploadFloor(MultipartFile file) {
        String originalFilename = file.getOriginalFilename();
        System.out.println("上传监控画面" + originalFilename);
        String s = null;
        try {
            s = OssUtil2.updateLoad(file.getInputStream(), originalFilename);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.ok().data("url",s);
    }

    @RequestMapping("updFloor")
    public R updFloor (FloorCameraPojo floorCameraPojo) {
        System.out.println(floorCameraPojo);
        return fctService.updFloor(floorCameraPojo);
    }

    @RequestMapping("/delSFloor")
    public R delSFloor (@RequestParam Map<String,Object> arrData) {
        return fctService.deletePetAll(arrData);
    }

    @RequestMapping("/barTable")
    public R barTable () {
        return fctService.brandBarData();
    }

    @RequestMapping("/pieTable")
    public R pieTable () {
        return fctService.brandPieData();
    }

    @RequestMapping("/export")
    public void export (FloorCameraPojo floor, HttpServletResponse response) throws IOException {
        //文件的下载   关键点 要设置 响应头

        //下载 excel文件 设置响应头
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        //防止中文乱码
        String filename = URLEncoder.encode("学生信息导出", "utf-8");

        response.setHeader("Content-Disposition","attachment;filename="+filename+".xlsx");


        // 得到 输出流 输出流向浏览器输出数据
        ServletOutputStream outputStream = response.getOutputStream();

        // easyexcel 写数据到 输出流
        ExcelWriterBuilder write = EasyExcel.write(outputStream, CameraBo.class);

        ExcelWriterSheetBuilder sheet = write.sheet();

        //准备列表数据
        List<CameraBo> students = fctService.studentExcelData(floor);
        sheet.doWrite(students);
    }
        @RequestMapping("/import")
    public R importExcel(MultipartFile file) throws IOException {
        /**
         * 参数1  文件流
         * 参数2 读取到的数据封装到哪个类型
         * 参数3 监听器  每读取一行数据 都会调用监听器的 invoke 方法
         *
         * */

        ExcelReaderBuilder read = EasyExcel.read(file.getInputStream(), CameraBo.class, webFloorListener);
        //得到 读取的sheet页对象
        ExcelReaderSheetBuilder sheet = read.sheet();
        //读取sheet
        sheet.doRead();

        return R.ok();
    }

    //模板下载
    @RequestMapping("/templateDownload")
    public void templateDownload(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("application/vnd.ms-excel");

        response.setCharacterEncoding("utf-8");
        String realPath = request.getServletContext().getRealPath("/camera/监控信息模板.xlsx");
        File file = new File(realPath);
        //将文件转为一个字节数组
        byte[] bytes = FileUtils.readFileToByteArray(file);
        //防止中文乱码
        String filename = URLEncoder.encode("保安信息模板", "utf-8");
        //
        response.setHeader("Content-Disposition","attachment;filename="+filename+".xlsx");
        ServletOutputStream outputStream = response.getOutputStream();
        outputStream.write(bytes);
        outputStream.close();
    }
}
